package com.bsj.pay.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsj.pay.common.Result;
import com.bsj.pay.config.GlobalConfig;
import com.bsj.pay.model.ConfigBean;
import com.bsj.pay.service.ChargeAccountService;
import com.bsj.pay.service.ConfigService;
import com.bsj.pay.service.LogService;
import com.bsj.pay.service.MessageService;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.util.date.DateTimeUtil;

@Controller("adminConfigController")
@RequestMapping("/admin/config")
public class ConfigController {
	
	@Autowired
	private ConfigService configService;
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private ChargeAccountService chargeAccountService;
	
	@RequestMapping("/listPage.html")
    public ModelAndView listPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		logService.addLog(request, "查看系统配置");
		ConfigBean queryModel=new ConfigBean();
		List<ConfigBean> configs=configService.queryList(queryModel);
		mav.addObject("configs", configs);
		mav.setViewName("admin/config/list");
		return mav;
    }
	
	
	@ResponseBody
	@RequestMapping(value = "/update.html")
	public Result update(HttpServletRequest req, HttpServletResponse res,ConfigBean config) {
		Result result=new Result();
		try {
			if(configService.update(config)>0)
			{
				GlobalConfig.setValue(config);
				logService.addLog(req, "修改系统配置:"+config.getName());
				result.setCode(0);
				result.setMsg("修改成功");
			}
			else
			{
				result.setCode(1);
				result.setMsg("修改失败");
			}
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
	
	
	@RequestMapping("/clearPage.html")
    public ModelAndView clearPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("admin/config/clear");
		return mav;
    }
	
	@ResponseBody
	@RequestMapping(value = "/clear.html")
	public Result clear(HttpServletRequest req, HttpServletResponse res,String startTimeStr,String endTimeStr,boolean order,boolean log,boolean charge,boolean msg) {
		Result result=new Result();
		try {
			long startTime=DateTimeUtil.strDate2UnixTime(startTimeStr+" 00:00:00");
			long endTime=DateTimeUtil.strDate2UnixTime(endTimeStr+" 00:00:00");
			if(order)
			{
				orderService.removeByTime(startTime, endTime);
			}
			if(charge)
			{
				chargeAccountService.removeByTime(startTime, endTime);
			}
			if(log)
			{
				logService.removeByTime(startTime, endTime);
			}
			if(msg)
			{
				messageService.removeByTime(startTime, endTime);
			}
			logService.addLog(req, "清理数据");
			result.setCode(0);
			result.setMsg("操作成功");
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
}
